/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AdministrarPrivilegio.java
 *
 * Created on 11-ene-2013, 12:07:07
 */
package inscripciones.vista;

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import org.jdesktop.beansbinding.BindingGroup;
import inscripciones.DAO.ConsultasBD;
import inscripciones.ModeloDAO.PrivilegioUsuarioDAO;
import inscripciones.Utiles.Mensajes;
import inscripciones.modelo.PrivilegioUsuario;


/**
 *
 * @author DJ2-PC
 */
public class AdministrarPrivilegio extends javax.swing.JDialog {
    PrivilegioUsuario privilegio; 
    ArrayList<PrivilegioUsuario> privilegios;
    ArrayList<PrivilegioUsuario> privilegiosbindig;
    BindingGroup b = null;
    NuevoUsuario nu;
    

    /** Creates new form AdministrarPrivilegio */
    public AdministrarPrivilegio(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        privilegios = (ArrayList<PrivilegioUsuario>)new ConsultasBD().buscarTodos("PrivilegioUsuario");
        nu=null;
        initComponents();        
    }

    public AdministrarPrivilegio(java.awt.Frame parent, boolean modal, BindingGroup b, NuevoUsuario nu ){
        super(parent, modal);
        privilegios = (ArrayList<PrivilegioUsuario>)new ConsultasBD().buscarTodos("PrivilegioUsuario");
        this.b=b;
        this.nu= nu;
        initComponents();        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        imagePanel1 = new com.mycsistemas.swingec.panel.ImagePanel();
        curvesPanel1 = new com.mycsistemas.swingec.panel.CurvesPanel();
        jLblAdmPrivilegio = new javax.swing.JLabel();
        jPanelAdministrarPrivilegio = new javax.swing.JPanel();
        jBtnNuevoPrivilegio = new javax.swing.JButton();
        jBtnEditarPrivilegio = new javax.swing.JButton();
        jBtnEliminarPrivilegio = new javax.swing.JButton();
        jBtnBusPrivilegio = new javax.swing.JButton();
        jTxtBuscarPrivilegioAdm = new javax.swing.JTextField();
        jLblBusPrivilegio = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTblPrivilegios = new javax.swing.JTable();
        jBtnCancelarPrivilegio = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Administrar Privilegio");

        imagePanel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/''_10102012_1738.jpeg"))); // NOI18N
        imagePanel1.setLayout(null);

        curvesPanel1.setLayout(null);

        jLblAdmPrivilegio.setFont(new java.awt.Font("Georgia", 3, 18));
        jLblAdmPrivilegio.setForeground(new java.awt.Color(255, 255, 255));
        jLblAdmPrivilegio.setText("Administrar Privilegio");
        curvesPanel1.add(jLblAdmPrivilegio);
        jLblAdmPrivilegio.setBounds(228, 32, 217, 21);

        jPanelAdministrarPrivilegio.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Tabla Administrar Privilegio", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Arial", 0, 10), new java.awt.Color(255, 255, 255))); // NOI18N
        jPanelAdministrarPrivilegio.setOpaque(false);
        jPanelAdministrarPrivilegio.setLayout(null);

        jBtnNuevoPrivilegio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/document_32.png"))); // NOI18N
        jBtnNuevoPrivilegio.setText("Nuevo");
        jBtnNuevoPrivilegio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnNuevoPrivilegioActionPerformed(evt);
            }
        });
        jPanelAdministrarPrivilegio.add(jBtnNuevoPrivilegio);
        jBtnNuevoPrivilegio.setBounds(470, 20, 130, 40);

        jBtnEditarPrivilegio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/Pencil3.png"))); // NOI18N
        jBtnEditarPrivilegio.setText("Editar");
        jBtnEditarPrivilegio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEditarPrivilegioActionPerformed(evt);
            }
        });
        jPanelAdministrarPrivilegio.add(jBtnEditarPrivilegio);
        jBtnEditarPrivilegio.setBounds(470, 70, 130, 41);

        jBtnEliminarPrivilegio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/trash_32.png"))); // NOI18N
        jBtnEliminarPrivilegio.setText("Eliminar");
        jBtnEliminarPrivilegio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnEliminarPrivilegioActionPerformed(evt);
            }
        });
        jPanelAdministrarPrivilegio.add(jBtnEliminarPrivilegio);
        jBtnEliminarPrivilegio.setBounds(470, 120, 130, 40);

        jBtnBusPrivilegio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/search_32.png"))); // NOI18N
        jBtnBusPrivilegio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnBusPrivilegioActionPerformed(evt);
            }
        });
        jPanelAdministrarPrivilegio.add(jBtnBusPrivilegio);
        jBtnBusPrivilegio.setBounds(390, 20, 52, 30);

        jTxtBuscarPrivilegioAdm.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTxtBuscarPrivilegioAdmKeyPressed(evt);
            }
        });
        jPanelAdministrarPrivilegio.add(jTxtBuscarPrivilegioAdm);
        jTxtBuscarPrivilegioAdm.setBounds(80, 20, 300, 30);

        jLblBusPrivilegio.setFont(new java.awt.Font("Elephant", 0, 14));
        jLblBusPrivilegio.setForeground(new java.awt.Color(255, 255, 255));
        jLblBusPrivilegio.setText("Buscar:");
        jPanelAdministrarPrivilegio.add(jLblBusPrivilegio);
        jLblBusPrivilegio.setBounds(15, 12, 54, 40);

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${privilegios}");
        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, eLProperty, jTblPrivilegios);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${id}"));
        columnBinding.setColumnName("Title 1");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nombre}"));
        columnBinding.setColumnName("Title 2");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${acceso}"));
        columnBinding.setColumnName("Acceso");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        jScrollPane1.setViewportView(jTblPrivilegios);

        jPanelAdministrarPrivilegio.add(jScrollPane1);
        jScrollPane1.setBounds(30, 70, 420, 140);

        jBtnCancelarPrivilegio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/inscripciones/Recursos/DeleteRed.png"))); // NOI18N
        jBtnCancelarPrivilegio.setText("Salir");
        jBtnCancelarPrivilegio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnCancelarPrivilegioActionPerformed(evt);
            }
        });
        jPanelAdministrarPrivilegio.add(jBtnCancelarPrivilegio);
        jBtnCancelarPrivilegio.setBounds(470, 170, 130, 40);

        curvesPanel1.add(jPanelAdministrarPrivilegio);
        jPanelAdministrarPrivilegio.setBounds(30, 60, 625, 230);

        imagePanel1.add(curvesPanel1);
        curvesPanel1.setBounds(0, 0, 670, 370);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 670, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(imagePanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 366, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jBtnBusPrivilegioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnBusPrivilegioActionPerformed
// TODO add your handling code here:
    if(jTxtBuscarPrivilegioAdm.getText().length()!=0){
        bindingGroup.unbind();
        privilegios = (ArrayList<PrivilegioUsuario>) new ConsultasBD().buscarLIKE("PrivilegioUsuario", "nombre", jTxtBuscarPrivilegioAdm.getText());
        bindingGroup.bind(); 
    }else{
        bindingGroup.unbind();
        privilegios = (ArrayList<PrivilegioUsuario>) new ConsultasBD().buscarTodos("PrivilegioUsuario");
        bindingGroup.bind();  
    }
}//GEN-LAST:event_jBtnBusPrivilegioActionPerformed

private void jBtnNuevoPrivilegioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnNuevoPrivilegioActionPerformed
// TODO add your handling code here:
    NuevoPrivilegio np= new NuevoPrivilegio(null,true, bindingGroup, privilegios);
    np.setLocationRelativeTo(this);
    np.setTitle("Nuevo Privilegio");
    np.setVisible(true);
    
}//GEN-LAST:event_jBtnNuevoPrivilegioActionPerformed

private void jBtnCancelarPrivilegioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnCancelarPrivilegioActionPerformed
// TODO add your handling code here:
    dispose();
    
    if(nu!= null){
        nu.dispose();
        nu = new NuevoUsuario(null, true, (ArrayList<PrivilegioUsuario>)new ConsultasBD().buscarTodos("PrivilegioUsuario"));
        nu.setNu(nu);
        nu.setTitle("Nuevo Usuario");
        nu.setLocationRelativeTo(this);
        nu.setVisible(true);
    }
        
}//GEN-LAST:event_jBtnCancelarPrivilegioActionPerformed

private void jBtnEditarPrivilegioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEditarPrivilegioActionPerformed
// TODO add your handling code here:
 
    if(jTblPrivilegios.getSelectedRow()!=-1){

        Integer idpriv=(Integer)jTblPrivilegios.getValueAt(jTblPrivilegios.getSelectedRow(), 0);
        ArrayList<PrivilegioUsuario> paux = (ArrayList<PrivilegioUsuario>)new ConsultasBD().buscarPOR("PrivilegioUsuario", "id", String.valueOf(idpriv));
        this.privilegio=paux.get(0);
        NuevoPrivilegio np = new NuevoPrivilegio(null, true, bindingGroup, privilegio);
        np.setLocationRelativeTo(this);
        np.setTitle("Editar Privilegio");
        np.setVisible(true);
    }else{
        new Mensajes().StandarWarning(this,"Seleccione el Privilegio que Desea Editar");
    }
    
}//GEN-LAST:event_jBtnEditarPrivilegioActionPerformed

private void jBtnEliminarPrivilegioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnEliminarPrivilegioActionPerformed
// TODO add your handling code here:
    
    if(jTblPrivilegios.getSelectedRow()!=-1){
        Integer idpriv=(Integer)jTblPrivilegios.getValueAt(jTblPrivilegios.getSelectedRow(), 0);
        ArrayList<PrivilegioUsuario> paux = (ArrayList<PrivilegioUsuario>)new ConsultasBD().buscarPOR("PrivilegioUsuario", "id", String.valueOf(idpriv));
        this.privilegio=paux.get(0);
        if(new Mensajes().ConfirmacionDialogowarning(this, "¿Está seguro de Eliminar éste registro?")==0){
                try {
                    PrivilegioUsuarioDAO priDAO = new PrivilegioUsuarioDAO(privilegio);
                            priDAO.update();
                            priDAO.remove();   
                            new Mensajes().ConfirmacionDialogotick(this, "El Registro se ha Eliminado Con Éxito");
                            this.bindingGroup.unbind();
                            privilegios.remove(this.privilegio);
                            this.bindingGroup.bind();       
                } catch (Exception ex) {
                   // ex.printStackTrace();
                    new Mensajes().StandarWarning(this, "                 No es Posible Eliminar. \n Éste Registro está Siendo Utilizado por el Sistema");
                }
                
        }
    }else{
        new Mensajes().StandarWarning(this,"Seleccione el Privilegio que Desea Eliminar");
    }
}//GEN-LAST:event_jBtnEliminarPrivilegioActionPerformed

private void jTxtBuscarPrivilegioAdmKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTxtBuscarPrivilegioAdmKeyPressed
// TODO add your handling code here:
     if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
        jBtnBusPrivilegio.requestFocus();
        jBtnBusPrivilegioActionPerformed(null);
    }
}//GEN-LAST:event_jTxtBuscarPrivilegioAdmKeyPressed

    /**
     * @param args the command line arguments
     */


    public PrivilegioUsuario getPrivilegio() {
        return privilegio;
    }

    public void setPrivilegio(PrivilegioUsuario privilegio) {
        this.privilegio = privilegio;
    }

    public ArrayList<PrivilegioUsuario> getPrivilegios() {
        return privilegios;
    }

    public void setPrivilegios(ArrayList<PrivilegioUsuario> privilegios) {
        this.privilegios = privilegios;
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.mycsistemas.swingec.panel.CurvesPanel curvesPanel1;
    private com.mycsistemas.swingec.panel.ImagePanel imagePanel1;
    private javax.swing.JButton jBtnBusPrivilegio;
    private javax.swing.JButton jBtnCancelarPrivilegio;
    private javax.swing.JButton jBtnEditarPrivilegio;
    private javax.swing.JButton jBtnEliminarPrivilegio;
    private javax.swing.JButton jBtnNuevoPrivilegio;
    private javax.swing.JLabel jLblAdmPrivilegio;
    private javax.swing.JLabel jLblBusPrivilegio;
    private javax.swing.JPanel jPanelAdministrarPrivilegio;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTblPrivilegios;
    private javax.swing.JTextField jTxtBuscarPrivilegioAdm;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
